package ethan.springCloud.controller;

import feign.ethan.springCloud.pojo.Dept;
import ethan.springCloud.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ethan
 */
@RestController
@RequestMapping(value = "/dept")
@Slf4j
public class DeptController {
    @Resource
    private DeptService service;

    @Resource
    private DiscoveryClient client;

    @PostMapping("/insert")
    public Boolean insert(Dept dept) {
        return service.insert(dept);
    }

    @GetMapping("detail/{id}")
    public Dept selectOne(@PathVariable("id") Integer id) {
        return service.selectOne(id);
    }

    @GetMapping("/all")
    public List<Dept> listAll() {
        return service.listAll();
    }

    @DeleteMapping("/delete/{id}")
    public Integer delete(@PathVariable("id") Integer id) {
        return service.delete(id);
    }

    @GetMapping("/discovery")
    public Object discovery() {
        //获取服务清单
        List<String> services = client.getServices();
        log.info("服务清单==>>{}", services);

        //获取实例信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        instances.forEach(it ->{
            log.info("--->>>id {}", it.getServiceId());
            log.info("--->>>uri {}", it.getUri());
            log.info("--->>>host {}", it.getHost());
            log.info("--->>>post {}", it.getPort());
        });
        return client;
    }
}
